﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InsideDL.MISService;
using InsideDL.MISControl;
using InsideDL.MISData;



namespace InsideDL
{
    public partial class frmIG005 : MISControl.frmMatrix
    {
        EXLAPP exlAPP = null;

        public frmIG005()
        {
            InitializeComponent();
            Utility.LoadConfigValue("frmIG005");
            this.controlList = new List<ucSelFileOrPath>
            {
                ucFYP,ucFinal
            };
            ucExecute1.ExecuteClick += new EventHandler(ucClickFacade);
            ucExecute_Exit.ExecuteClick += new EventHandler(ucClickFacade);

            //把預設的先填入
            ucCMRange.Text = Utility.GetConfigValue("sourceRangeCM");
            ucYTDRange.Text = Utility.GetConfigValue("sourceRangeYTD");

        }



        protected override void execute()
        {
            if (base.Vaildation().Equals(false))
            { return; }

            //取出資料
            string cos, targetShtCM, sourceSht;
            //目前sheet 和檔案名相同
            sourceSht = System.IO.Path.GetFileNameWithoutExtension(ucFinal.Txt);
            cos = Utility.GetConfigValue("cos");
            targetShtCM = Utility.GetConfigValue("targetShtCM");
            DataIG004 ig004 = new DataIG004();
            exlAPP = new EXLAPP();
            exlAPP.OpenFile(ucFinal.Txt);
            exlAPP.OpenFile(ucFYP.Txt);

            //取得公司list，要以這為基準
            string[] coList = ig004.GetInsuranceCO(
                exlAPP.GetSheetRangeArray(targetShtCM, cos));
            coList[2] = "中國"; //2012-04-30 把來源中國人壽置換成中國
            //最得所有YYYMM的欄位字串陣列
            string[] yearMonth = ig004.GetInsuranceCO(
                exlAPP.GetSheetRangeArray(targetShtCM
                , Utility.GetConfigValue("yearMonth")),null);
            int col = Array.IndexOf(yearMonth, sourceSht);
            //撈出資料，取出當月的通路資料和累計到當月的資料
            DataTable cmDT = ig004.GetRoute(exlAPP.GetSheetRangeArray(sourceSht
                , ucCMRange.Text)
                , new[] { 2,5,8,11});


            DataTable ytdDT = ig004.GetRoute(
                exlAPP.GetSheetRangeArray(sourceSht
                , ucYTDRange.Text)
                ,new [] {2,4,6,8}
                );
            DataTable[] dts = { cmDT,ytdDT };

            
            string[] sheets = { Utility.GetConfigValue("targetShtCM")
                               ,Utility.GetConfigValue("targetShtYTD") };
            int next,tmp;
            next = 15;
            tmp = 0;

            for (int i = 0; i < sheets.Length; i++)
            {
                int startRow = Convert.ToInt16(Utility.GetConfigValue("startRow"));
                //因為每一個sheet 會有四個區塊
                foreach (string co in coList)
                {
                    IQueryable<DataRow> incomeRow = ig004.GetSeleIncome(co, dts[i]);
                    int colPos = Array.IndexOf(yearMonth, sourceSht) + 3;
                    tmp = startRow;
                    foreach (DataRow row in incomeRow)
                    {
                        exlAPP.SetRangeValue(sheets[i], tmp, colPos
                                , row[1].ToString());
                        tmp += next;
                    }
                    startRow++;
                }
            }
            exlAPP.SaveCloseXLS();
            exlAPP = null;
            showMessage(this.Text, "完成！！");            
        }
    }
}
